package com.lik.javadeep;

import lombok.Data;
import org.springframework.util.MultiValueMap;

import java.util.Comparator;
import java.util.Objects;
import java.util.TreeMap;

public class TreeMapTest {
    public static void main(String[] args) {
        TreeMap<Person, Object> map = new TreeMap<>();

        TreeMap<Person, Object> map2 = new TreeMap<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.age - o2.age;
            }
        });

        for (int i = 0; i < 5; i++) {
            Person person = new Person(i);
            map.put(person, i);
            map2.put(person, i);
        }
        map.forEach((k, v) -> System.out.println(k));
        System.out.println("============");
        map2.forEach((k, v) -> System.out.println(k));

    }

    @Data
    static class Person implements Comparable<Person> {
        private int age;
        private String name;

        public Person(int age) {
            this.age = age;
        }

        @Override
        public int compareTo(Person o) {

            return o.age - this.age;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Person person = (Person) o;
            return age == person.age &&
                    Objects.equals(name, person.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(age, name);
        }
    }
}
